﻿using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Padsurface.App.Tests.Unit.Stubs;
using Padsurface.App.Views;
using Padsurface.Audio.Song.Model;
using System.Collections.Generic;
using System.Linq;
using Windows.Storage;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Padsurface.App.Tests.Unit.ViewModels.StartViewModelSpecifications
{
    public abstract class WhenBrowseSong : StartViewModelTestBase
    {
        protected object ExecuteParameter { get; set; }

        protected override void Arrange()
        {
            base.Arrange();
        }

        protected override void Act()
        {
            EventTrigger = new ManualResetEvent(false);

            if (ViewModel.BrowseSongCommand.CanExecute(ExecuteParameter))
                ViewModel.BrowseSongCommand.Execute(ExecuteParameter);

            EventTrigger.WaitOne(TimeSpan.FromSeconds(3));
            EventTrigger.Set();
        }
    }

    [TestClass]
    public class WhenBrowseGivenEmptyRecentSongCacheAndCancelled : WhenBrowseSong
    {
        private Song _currentActiveSong;

        protected override void Arrange()
        {
            base.Arrange();

            FilePicker.OpenSingleSongFileAsyncResult = null;

            _currentActiveSong = new Song(new MetronomeStub(), new PlayBufferStub());
            ActiveSongCache.ActiveSong = _currentActiveSong;

            RecentSongCache.LoadResult = new List<Padsurface.App.ViewModels.Start.SongProjectViewModel>();
        }

        [TestMethod]
        public void ThenActiveSongShouldNotBeChanged()
        {
            Assert.AreEqual(_currentActiveSong, ActiveSongCache.ActiveSong);
        }

        [TestMethod]
        public void ThenRecentSongsShouldStillBeEmpty()
        {
            Assert.AreEqual(0, RecentSongCache.Load().Count());
        }
    }

    [TestClass]
    public class WhenBroseGivenEmptyRecentSongCacheAndSongSelected : WhenBrowseSong
    {
        private string _fileNamePart = "unit-test";

        protected async override void Arrange()
        {
            base.Arrange();

            var dummyFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(_fileNamePart, CreationCollisionOption.GenerateUniqueName);
            FilePicker.OpenSingleSongFileAsyncResult = dummyFile;
        }

        [TestMethod]
        public void ThenSongSelectedSongShouldBeActive()
        {
            Assert.IsTrue(ActiveSongCache.ActiveSong.Name.Contains(_fileNamePart));
        }

        [TestMethod]
        public void ThenMostRecentSongShouldBeSelectedSong()
        {
            Assert.IsTrue(RecentSongCache.Load().First().FilePath.Contains(_fileNamePart));
        }

        [TestMethod]
        public void ThenAppShouldNavigateToSong()
        {
            Assert.IsTrue((ViewModel.NavigationService as NavigationServiceStub).NavigatedPages.Contains(typeof(PadPage)));
        }
    }
}